﻿Option Strict Off
Option Explicit On

Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic.Compatibility.VB6

<ProgId("SrmConector_NET.SrmConector")> _
Public Class SrmConector
    Private Declare Function Srmw32 Lib "c:\bin\f\Srmw32.dll" (ByVal Nodo As String, ByVal Apli As String, _
                                                               ByVal msg As String, ByRef Largo As Integer, _
                                                               ByVal Status As String, ByVal funcion As String, _
                                                               ByVal Contexto As String, ByVal Control As String) _
        As Short

    Private m_ActivarLog As Boolean
    Private m_ArchivoLog As String

    Private m_RetornoAplicacion As New FixedLengthString(2)
    Private m_RetornoSrm As Integer
    Private m_StatusSrm As Short
    Private m_Respuesta As String

    Private m_Mensaje As String
    Private m_Aplicacion As String
    Private m_Nodo As String
    Private m_Tipo As String
    Private m_Contexto As String

    Public Function Invoke() As Boolean

        On Error GoTo 0
        On Error GoTo Err_Srm

        Dim strNowInicio As String
        Dim strResponse As New FixedLengthString(8166)
        Dim strStatus As New FixedLengthString(2)
        Dim strTipo As New FixedLengthString(2)
        ' Funcion.
        Dim strContexto As New FixedLengthString(2)
        ' Contexto.
        Dim strControl As String
        ' Control.
        Dim lngLength As Integer
        ' Largo del Mensaje.
        Dim NF As Short

        If m_ActivarLog Then
            strNowInicio = Format(Now, "YYYYMMDDHHMMSS")
        End If

        strStatus.Value = "00"
        strControl = Space(10000)

        If Trim(m_Contexto) = "" Then
            strContexto.Value = "00"
        Else
            strContexto.Value = Trim(m_Contexto)
        End If

        If Trim(m_Tipo) = "" Then
            strTipo.Value = "08"
        Else
            strTipo.Value = Trim(m_Tipo)
        End If

        strResponse.Value = m_Mensaje & Chr(0)
        lngLength = Len(m_Mensaje)

        m_RetornoSrm = _
            Srmw32(m_Nodo, m_Aplicacion, strResponse.Value, lngLength, strStatus.Value, strTipo.Value, _
                    strContexto.Value, strControl)

        m_Respuesta = Mid(strResponse.Value, 1, lngLength)
        m_StatusSrm = Val(strStatus.Value)

        If lngLength >= 2 Then
            m_RetornoAplicacion.Value = Mid(m_Respuesta, 1, 2)
        End If

        If m_ActivarLog Then

            On Error GoTo 0
            On Error Resume Next

            NF = FreeFile
            FileOpen(NF, m_ArchivoLog, OpenMode.Append)

            Print(NF, strNowInicio)
            Print(NF, Format(Now, "YYYYMMDDHHMMSS"))
            Print(NF, m_Nodo)
            Print(NF, m_Aplicacion)
            Print(NF, strTipo.Value & Space(2 - Len(strTipo.Value)))
            Print(NF, Format(Len(m_Mensaje), "0000"))
            Print(NF, m_Mensaje)
            Print(NF, Format(m_RetornoSrm, "0000"))
            Print(NF, Format(Val(strStatus.Value), "0000"))
            Print(NF, Format(Len(m_Respuesta), "0000"))
            PrintLine(NF, m_Respuesta)

            FileClose(NF)

            On Error GoTo 0
        End If

Exit_Srm:
        On Error Resume Next

        If m_RetornoSrm = 0 Then
            If IsNumeric(m_RetornoAplicacion.Value) Then
                If Val(m_RetornoAplicacion.Value) = 0 Then
                    Invoke = True
                Else
                    Invoke = False
                End If
            Else
                Invoke = False
            End If
        Else
            Invoke = False
        End If

        Exit Function

Err_Srm:
        GoTo Exit_Srm
    End Function


    Public Property Nodo() As String
        Get
            Nodo = m_Nodo
        End Get
        Set(ByVal Value As String)
            m_Nodo = Trim(Value)
        End Set
    End Property


    Public Property Aplicacion() As String
        Get
            Aplicacion = m_Aplicacion
        End Get
        Set(ByVal Value As String)
            m_Aplicacion = Trim(Value)
        End Set
    End Property


    Public Property Mensaje() As String
        Get
            Mensaje = m_Mensaje
        End Get
        Set(ByVal Value As String)
            m_Mensaje = Value
        End Set
    End Property


    Public Property Tipo() As String
        Get
            Tipo = m_Tipo
        End Get
        Set(ByVal Value As String)
            m_Tipo = Value
        End Set
    End Property


    Public Property Contexto() As String
        Get
            Contexto = m_Contexto
        End Get
        Set(ByVal Value As String)
            m_Contexto = Value
        End Set
    End Property


    'Definicion de propiedades de solo lectura

    Public Property Respuesta() As String
        Get
            Respuesta = m_Respuesta
        End Get
        Set(ByVal Value As String)
        End Set
    End Property


    Public Property StatusSrm() As Short
        Get
            StatusSrm = m_StatusSrm
        End Get
        Set(ByVal Value As Short)
        End Set
    End Property


    Public Property RetornoSrm() As Short
        Get
            RetornoSrm = m_RetornoSrm
        End Get
        Set(ByVal Value As Short)
        End Set
    End Property


    Public Property RetornoAplicacion() As String
        Get
            RetornoAplicacion = m_RetornoAplicacion.Value
        End Get
        Set(ByVal Value As String)
        End Set
    End Property


    Public Property ActivarLog() As Boolean
        Get
            ActivarLog = m_ActivarLog
        End Get
        Set(ByVal Value As Boolean)
            m_ActivarLog = Value
        End Set
    End Property


    Public Property ArchivoLog() As String
        Get
            ArchivoLog = m_ArchivoLog
        End Get
        Set(ByVal Value As String)
            m_ArchivoLog = Value
        End Set
    End Property


    'UPGRADE_NOTE: Class_Initialize se actualizó a Class_Initialize_Renamed. Haga clic aquí para obtener más información: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
    Private Sub Class_Initialize_Renamed()
        'Dim strActivarLog As String

        'strActivarLog = GetConfiguracion("SrmConector", "ActivarLog")
        'm_ArchivoLog = GetConfiguracion("SrmConector", "ArchivoLog")

        'If UCase(Trim(strActivarLog)) = "SI" Then
        '    m_ActivarLog = True
        'Else
        '    m_ActivarLog = False
        'End If

    End Sub

    Public Sub New()
        MyBase.New()
        Class_Initialize_Renamed()
    End Sub
End Class